import * as api from '@/api/user';

const state = {
  token: uni.getStorageSync('token') || '',
  userInfo: uni.getStorageSync('userInfo') || null
};

const mutations = {
  SET_TOKEN(state, token) {
    state.token = token;
    uni.setStorageSync('token', token);
  },
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo;
    uni.setStorageSync('userInfo', userInfo);
  },
  CLEAR_USER(state) {
    state.token = '';
    state.userInfo = null;
    uni.removeStorageSync('token');
    uni.removeStorageSync('userInfo');
  }
};

const actions = {
  // 用户登录
  login({ commit }, data) {
    return new Promise((resolve, reject) => {
      api.login(data)
        .then(res => {
          commit('SET_TOKEN', res.data.token);
          commit('SET_USER_INFO', res.data.user);
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 用户注册
  register({ commit }, data) {
    return new Promise((resolve, reject) => {
      api.register(data)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 退出登录
  logout({ commit }) {
    return new Promise(resolve => {
      api.logout()
        .then(() => {
          commit('CLEAR_USER');
          resolve();
        })
        .catch(() => {
          commit('CLEAR_USER');
          resolve();
        });
    });
  },

  // 验证token是否有效
  validateToken({ commit }) {
    return new Promise((resolve, reject) => {
      api.validateToken()
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          commit('CLEAR_USER');
          reject(err);
        });
    });
  },

  // 获取用户信息
  getUserInfo({ commit }) {
    return new Promise((resolve, reject) => {
      api.getUserInfo()
        .then(res => {
          commit('SET_USER_INFO', res.data);
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 更新用户信息
  updateUserInfo({ commit }, data) {
    return new Promise((resolve, reject) => {
      api.updateUserInfo(data)
        .then(res => {
          commit('SET_USER_INFO', res.data);
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 更新用户头像
  updateAvatar({ commit }, filePath) {
    return new Promise((resolve, reject) => {
      api.updateAvatar(filePath)
        .then(res => {
          const userInfo = state.userInfo;
          userInfo.avatar = res.data.url;
          commit('SET_USER_INFO', userInfo);
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 修改密码
  changePassword({ commit }, data) {
    return new Promise((resolve, reject) => {
      api.changePassword(data)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 重置密码
  resetPassword({ commit }, data) {
    return new Promise((resolve, reject) => {
      api.resetPassword(data)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  }
};

const getters = {
  token: state => state.token,
  userInfo: state => state.userInfo,
  isLogin: state => !!state.token,
  isAdmin: state => state.userInfo && state.userInfo.role === 'admin',
  isCoach: state => state.userInfo && state.userInfo.role === 'coach',
  isStudent: state => state.userInfo && state.userInfo.role === 'student'
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
};